package com.rainng.coursesystem.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.CheckFileByPrjMapper;
import com.rainng.coursesystem.dao.mapper.CheckFileMapper;
import com.rainng.coursesystem.manager.OptionManager;
import com.rainng.coursesystem.model.entity.RoleEntity;
import com.rainng.coursesystem.model.shitong.*;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.rainng.coursesystem.model.constant.GeneralReportProjectMap.ONLINE_PRJ_LIST;

@Service
public class CheckFileService extends BaseService {

    @Resource
    private CheckFileMapper mapper;

    @Resource
    private CheckFileByPrjMapper prjMapper;

    @Resource
    private UserService userService;

    public ResultVO readFileCache(PdfFileCacheVo vo) {
        int i = mapper.countFileInfoCache(vo.getPrjNum(), vo.getFilename());
        if (i == 0) {
            return result("");
        }
        String res = mapper.getFileInfoCache(vo.getPrjNum(), vo.getFilename());
        return result(res);
    }

    public ResultVO deleteFile(PdfFileCacheVo vo) {
        int i = mapper.deleteFileInfoCache(vo.getPrjNum(), vo.getFilename());
        return result("删除成功");
    }

    public ResultVO writeFileCache(PdfFileCacheVo vo) {


        int i = mapper.countFileInfoCache(vo.getPrjNum(), vo.getFilename());

//        //todo 日志相关 提过一次文件操作记录需求
//        if(vo.getPrjNum() != null){
//            ResultVO<Map<String, String>> userinfo = userService.getLoginStatusWithDepartment();
//            String username = userinfo.getData().get("username");
//            if(username != null){
//                String thetime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//                String rs = "用户"+username+"于"+thetime +"进行了修改";
//                int k = mapper.updateFileInfoCacheLog(vo.getPrjNum(), vo.getFilename(), rs);
//            }
//        }

        if (i == 0) {
            Map<String, String> m = vo.getInfo();
            String json = JSONUtil.toJsonStr(m);
            int j = mapper.saveFileInfoCache(vo.getPrjNum(), vo.getFilename(), json);
            return result("暂存成功");
        }
        Map<String, String> m = vo.getInfo();
        String json = JSONUtil.toJsonStr(m);
        int j = mapper.updateFileInfoCache(vo.getPrjNum(), vo.getFilename(), json);
        return result("暂存成功");
    }


    public ResultVO getNaCode(Map<String, String> m) {
        List<String> mp = mapper.getNACodes();
        return result(mp);
    }



    public List<Map<String, String>> getCodesByPrjNum(CheckFileCodesVo vo) {
        String veType = vo.getVeType();
        String vt = "zaihuoqiche";
        if ("半挂车".equals(veType)) {
            vt = "guache";
        }
        if ("牵引车辆".equals(veType)) {
            vt = "qianyinche";
        }
        if(StrUtil.isEmpty(vo.getPrjNum())){
            List<Map<String, String>> r = mapper.getAllCodes(vt);
            return r;
        }
        List<Map<String, String>> res = mapper.getCodesByPrj(vo.getPrjNum(), vt);
        return res;
    }
    public List<Map<String, String>> getDicCodes() {
        List<Map<String, String>> res = mapper.getDicCodes();
        return res;
    }
    public Map<String, String> selectFileInfoById(CheckFileDetailByNameVo vo) {
        String fname = vo.getFilename();
        String prjNum = vo.getPrjNum();
        if (!ONLINE_PRJ_LIST.contains(prjNum)){
            return new HashMap<>();
        }

        if (fname.endsWith(".docx") || fname.endsWith(".pdf")) {
            Map<String, String> mark0 = mapper.selectFileInfoByPrjNumAndFilename(prjNum, fname);
            if (mark0 != null && mark0.size() > 0) {
                return mark0;
            }
        }
        //如果先查pdf
        List<Map<String, String>> mark1 = mapper.selectFileInfoByPrjNumAndFname(prjNum, fname);
        if (mark1 != null && mark1.size() > 0) {
            return mark1.get(0);
        }
        return new HashMap<>();
    }

    public ResultVO confirmFileInfoById(CheckFileDetailVo vo) {
        Integer id = vo.getId();
        String prjNum = vo.getPrjNum();
        int i = mapper.confirmFileInfo(vo.getPrjNum(), vo.getId());
        return result("确认无误");
    }

    public ResultVO countCheckedFiles(CheckFileVo cvo) {
        if (StrUtil.isEmpty(cvo.getPrjNum())) {
            return result(0);
        }
        int i = mapper.countInfoByPrj(cvo);
        return result(i);
    }

    public List<Object> selectCheckedFiles(CheckFileVo cvo) {
        if (StrUtil.isEmpty(cvo.getPrjNum())) {
            return new ArrayList<>();
        }

        Page<Object> pageParam = new Page<>(cvo.getPageNum(), cvo.getPageSize());
        IPage<Object> o = mapper.queryInfoByPrj(cvo, pageParam);
        if (o == null) {
            return new ArrayList<>();
        }
        return o.getRecords();
    }

    public ResultVO updateCheckedFilesValidTime(CheckFileUpdateVo vo) {
        if (StrUtil.isEmpty(vo.getPrjNum())) {
            return failedResult("参数不能为空");
        }
//        String vtime = "";
//        try {
//            vtime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        int j = mapper.updateFileInfoByPrjNumAndFilename(vo.getPrjNum(), vo.getId(), vo.getValidtime());
        return result("修改成功");
    }

    public Object updateCheckedFilesInfo(Map<String, String> map) {
        String prjNum = map.get("prjNum");
        String filename = map.get("filename");
        String remark = map.get("remark");
        int num = mapper.countInfoByPrjAndFileName(prjNum, filename);
        //插入
        if (num == 0) {
            Integer filetype = 1;
            String veType = "载货汽车"; // 如果没识别处理,只能是按载货汽车处理了
            if (filename.startsWith("G")) {
                veType = "半挂车";
            }
            if (filename.startsWith("Q")) {
                veType = "牵引车辆";
            }
            if (filename.endsWith(".docx")) {
                filetype = 2;
            }
            String fname = "";
            if (filetype == 1) {
                fname = filename.split("\\.pdf")[0];
                String uploadtime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                long currentTime = System.currentTimeMillis();
                String info = filename;
//                int j = mapper.saveFileInfo(prjNum, filetype, filename, fname, veType, uploadtime);
                int j = mapper.saveFileInfoWithDocx(prjNum, filetype, filename, fname, info, veType, uploadtime, currentTime, remark);
            } else {
                fname = filename.split("\\.docx")[0];
                String uploadtime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                long currentTime = System.currentTimeMillis();
                String info = filename;
                //判断是否存在映射关系
                //List<String> ls = mapper.selectNameByDocxName(filename);
//                if (ls.size() > 0) {
//                    info = ls.get(0) + ".docx";
//                }
                if ("38".equals(prjNum)) {
                    int j1 = mapper.saveFileInfoWithDocx("38", filetype, filename, fname, info, veType, uploadtime, currentTime, remark);
                    int j2 = mapper.saveFileInfoWithDocx("382", filetype, filename, fname, info, veType, uploadtime, currentTime, remark);
                } else {
                    int j = mapper.saveFileInfoWithDocx(prjNum, filetype, filename, fname, info, veType, uploadtime, currentTime, remark);
                }
            }
//            String year = "";
//            year = filename.substring(2, 4);
//            if (fname.startsWith("HO")) {
//                year = filename.substring(3, 5);
//            }
        }

        int i = 0;
        if ("5".equals(prjNum)) {
            i = prjMapper.updateTreatInfo5(map);
        }
        if ("12".equals(prjNum)) {
            i = prjMapper.updateTreatInfo12(map);
        }
        if ("25".equals(prjNum)) {
            i = prjMapper.updateTreatInfo25(map);
        }
        if ("26".equals(prjNum)) {
            i = prjMapper.updateTreatInfo26(map);
        }
        if ("38".equals(prjNum)) {
            i = prjMapper.updateTreatInfo38(map);
        }
        if ("382".equals(prjNum)) {
            i = prjMapper.updateTreatInfo382(map);
        }
        if ("42".equals(prjNum)) {
            i = prjMapper.updateTreatInfo42(map);
        }
        if ("43".equals(prjNum)) {
            i = prjMapper.updateTreatInfo43(map);
        }
        if ("48".equals(prjNum)) {
            i = prjMapper.updateTreatInfo48(map);
        }
        if ("51".equals(prjNum)) {
            i = prjMapper.updateTreatInfo51(map);
        }
        //以下为实测
        if ("21".equals(prjNum)) {
            i = prjMapper.updateTreatInfo21(map);
        }
        if ("22".equals(prjNum)) {
            i = prjMapper.updateTreatInfo22(map);
        }
        if ("24".equals(prjNum)) {
            i = prjMapper.updateTreatInfo24(map);
        }
        if ("53".equals(prjNum)) {
            i = prjMapper.updateTreatInfo53(map);
        }
        if ("66".equals(prjNum)) {
            i = prjMapper.updateTreatInfo66(map);
        }
        //
        if ("2".equals(prjNum)) {
            i = prjMapper.updateTreatInfo2(map);
        }
        if ("3".equals(prjNum)) {
            i = prjMapper.updateTreatInfo3(map);
        }
        if ("4".equals(prjNum)) {
            i = prjMapper.updateTreatInfo4(map);
        }
        if ("11".equals(prjNum)) {
            i = prjMapper.updateTreatInfo11(map);
        }
        if ("27".equals(prjNum)) {
            i = prjMapper.updateTreatInfo27(map);
        }

        if ("29".equals(prjNum)) {
            i = prjMapper.updateTreatInfo29(map);
        }
        if ("30".equals(prjNum)) {
            i = prjMapper.updateTreatInfo30(map);
        }
        if ("31".equals(prjNum)) {
            i = prjMapper.updateTreatInfo31(map);
        }

        if ("37".equals(prjNum)) {
            i = prjMapper.updateTreatInfo37(map);
        }
        if ("45".equals(prjNum)) {
            i = prjMapper.updateTreatInfo45(map);
        }
        if ("46".equals(prjNum)) {
            i = prjMapper.updateTreatInfo46(map);
        }
        if ("47".equals(prjNum)) {
            i = prjMapper.updateTreatInfo47(map);
        }

        if ("54".equals(prjNum)) {
            i = prjMapper.updateTreatInfo54(map);
        }

        if ("55".equals(prjNum)) {
            i = prjMapper.updateTreatInfo55(map);
        }

        if ("57".equals(prjNum)) {
            i = prjMapper.updateTreatInfo57(map);
        }

        if ("58".equals(prjNum)) {
            i = prjMapper.updateTreatInfo58(map);
        }


        if ("63".equals(prjNum)) {
            i = prjMapper.updateTreatInfo63(map);
        }
        if ("64".equals(prjNum)) {
            i = prjMapper.updateTreatInfo64(map);
        }
        if ("65".equals(prjNum)) {
            i = prjMapper.updateTreatInfo65(map);
        }

        //实测项目
        if ("19".equals(prjNum)) {
            i = prjMapper.updateTreatInfo19(map);
        }
        if ("70".equals(prjNum)) {
            i = prjMapper.updateTreatInfo70(map);
        }
        if ("71".equals(prjNum)) {
            i = prjMapper.updateTreatInfo71(map);
        }
        if ("73".equals(prjNum)) {
            i = prjMapper.updateTreatInfo73(map);
        }
        if ("75".equals(prjNum)) {
            i = prjMapper.updateTreatInfo75(map);
        }
        if ("76".equals(prjNum)) {
            i = prjMapper.updateTreatInfo76(map);
        }
        if ("77".equals(prjNum)) {
            i = prjMapper.updateTreatInfo77(map);
        }
        if ("78".equals(prjNum)) {
            i = prjMapper.updateTreatInfo78(map);
        }
        if ("79".equals(prjNum)) {
            i = prjMapper.updateTreatInfo79(map);
        }
        if ("80".equals(prjNum)) {
            i = prjMapper.updateTreatInfo80(map);
        }
        if ("81".equals(prjNum)) {
            i = prjMapper.updateTreatInfo81(map);
        }
        if ("82".equals(prjNum)) {
            i = prjMapper.updateTreatInfo82(map);
        }
        if ("83".equals(prjNum)) {
            i = prjMapper.updateTreatInfo83(map);
        }
        if ("84".equals(prjNum)) {
            i = prjMapper.updateTreatInfo84(map);
        }
        if ("85".equals(prjNum)) {
            i = prjMapper.updateTreatInfo85(map);
        }

        if ("86".equals(prjNum)) {
            i = prjMapper.updateTreatInfo86(map);
        }
        //非实测项目

        return i;
    }

    // 项目,文件名,颜色
    public Map<String, Map<String, String>> checkFileColor(CheckFileColorVo cvo) {
        Map<String, String> prjInfos = cvo.getPrjInfo();
        Map<String, Map<String, String>> result = new HashMap<>();
        for (Map.Entry<String, String> entry : prjInfos.entrySet()) {
            Map<String, String> prj_result = new HashMap<>();
            String prjNum = entry.getKey();
            if (!ONLINE_PRJ_LIST.contains(prjNum)){
                continue;
            }

            String value_init = entry.getValue();
            String value = value_init.replace("，", ",");

            String[] fnames = value.split(",");
            if (fnames.length == 0) {
                continue;
            }

            for (int i = 0; i < fnames.length; i++) {
                String fname = fnames[i];
                if (fname == null || fname.trim().length() == 0) {
                    continue;
                }
                System.out.println(fname);
                //如果pdf或docx结尾
                if (fname.endsWith(".docx") || fname.endsWith(".pdf")) {
                    List<Integer> mark0 = mapper.selectIdentifiedMarkByFilename(prjNum, fname);
                    if (mark0.size() > 0) {
                        prj_result.put(fname,mark0.get(0).toString());
                        continue;
                    }
                }
                //如果先查pdf
                List<Integer> mark1 = mapper.selectIdentifiedMarkByFname(prjNum,fname);
                if (mark1.size() > 0) {
                    prj_result.put(fname,mark1.get(0).toString());
                    continue;
                }
//                List<Integer> mark2 = mapper.selectIdentifiedMarkByInfo(prjNum,fname);
//                if (mark2.size() > 0) {
//                    prj_result.put(fname,mark2.get(0).toString());
//                    continue;
//                }
                //再没有就查docx,info
                prj_result.put(fname,"2");
            }
            // 对于一个项目循环完,写入result
            result.put(prjNum,prj_result);
        }
        return result;
    }

}
